package com.lz.ionicbase.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

import com.lz.ionicbase.R;

public class TestActivity extends AppCompatActivity {

    private WebView mWebView;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        mWebView = (WebView) findViewById(R.id.webview);

        setWebView(mWebView, "");

//        mWebView.loadUrl("http://112.125.95.20:8585/project/mobile/notice/list.jsp");
//        mWebView.loadUrl("https://view.officeapps.live.com/op/view.aspx?src=http%3A%2F%2Fwww.medgrade.sdu.edu.cn%2Fupload%2F1298615889.doc");
//        mWebView.loadUrl("file:///android_asset/pdfjs/generic/web/viewer.html");
        mWebView.loadUrl("http://192.168.100.205:8080/pdf/web/viewer.html?file=test.pdf");

        editText = (EditText) findViewById(R.id.editText);
        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = editText.getText().toString().trim();
                mWebView.loadUrl(url);
            }
        });

    }

    private void setWebView(WebView mWebView, String textEncoding) {

        mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);// 滚动条风格，为0就是不给滚动条留空间，滚动条覆盖在网页上

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setDefaultTextEncodingName(textEncoding == null ? "UTF-8" : textEncoding);//设置编码
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(false);//设置显示缩放按钮
        webSettings.setSupportZoom(true); // 支持缩放
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//支持内容重新布局
        //自适应屏幕
        webSettings.setUseWideViewPort(true);//将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true);// 缩放至屏幕的大小

        webSettings.setLoadsImagesAutomatically(true);  //支持自动加载图片
        webSettings.setSupportMultipleWindows(true);// 设置允许开启多窗口

        webSettings.setDomStorageEnabled(true);// 开启 DOM storage API 功能
//        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//优先从本地cache中载入，其次才是从网络中载入，即使cache已经过期。
//        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不使用缓存
        webSettings.setAppCacheEnabled(true);       //开启 Application Caches 功能
        String cacheDirPath = getCacheDir().getAbsolutePath()+ "WebView";
        webSettings.setDatabasePath(cacheDirPath);  //设置数据库缓存路径
        webSettings.setAppCachePath(cacheDirPath);  //设置  Application Caches 缓存目录

        webSettings.setAllowFileAccess(true);  //设置可以访问文件
        //webSettings.setDefaultFontSize(16);//设置默认字体大小
//        mWebView.requestFocusFromTouch();//手势焦点

        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;//返回false,新链接地址使用WebView 加载该 url。否则使用系统浏览器等
            }
        });
//        mWebView.setWebChromeClient(myWebChromeClient);
//
//        mWebView.addJavascriptInterface(new JScriptObject(this), "androidObj");

//        mWebView.clearCache(true);//用于测试----清除 内存+磁盘 缓存
    }

}
